﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SQLite;

namespace DataAccess
{
	public class HelperDAL
	{
		private string connectionString = "Data Source=" + new System.IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\DoubleFeature").FullName + "\\Resources\\movies.sqlite";

		public void UpgradeSchema()
		{
			SQLiteConnection conn = new SQLiteConnection(connectionString);

			SQLiteCommand cmd = new SQLiteCommand("ALTER TABLE movies ADD movie_comments TEXT", conn);

			conn.Open();

			cmd.ExecuteNonQuery();

			cmd.CommandText = "ALTER TABLE movies ADD movie_views INT DEFAULT 0";

			cmd.ExecuteNonQuery();

			conn.Close();
		}

		public bool IsDatabaseUpgraded()
		{
			try
			{
				SQLiteConnection conn = new SQLiteConnection(connectionString);

				SQLiteCommand cmd = new SQLiteCommand("SELECT movie_comments, movie_views FROM movies", conn);

				conn.Open();

				SQLiteDataReader dr = cmd.ExecuteReader();

				bool hasrows = dr.HasRows;

				conn.Close();

				return true;
			}
			catch (Exception e)
			{
				return false;
			}
		}
	}
}
